package com.shuimin.service.model.sys;

import com.shuimin.service.App;
import com.shuimin.service.service.sys.ShopService;
import com.shuimin.table.XLSTable;
import pond.common.S;
import pond.common.STRING;
import pond.db.Model;
import pond.security.rbac.RBAC;
import pond.web.EndToEndException;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * Created by ed on 12/24/15.
 */
public class User extends Model {
  {
    table("t_user");
    id("id");
    field("uid");
    field("password");
    field("sex");
    field("name");
    field("shop_id");
    field("tel");
    field("email");
    field("rank");
  }

  public User refineShop() {
    if (this.get("_shop") == null) {
      String shop_id = this.get("shop_id");
      if (STRING.notBlank(shop_id))
        this.set("_shop", ShopService.get(shop_id));
    }
    return this;
  }

  public Shop shop() {
    Object _shop;
    if ((_shop = this.get("_shop")) == null) {
      refineShop();
    }
    if (_shop == null)
      throw new EndToEndException(400, "用户门店未设置");

    return this.get("_shop");
  }

  public User refineViewShops() {
    if (this.get("_view_shops") == null) {
      String user_id = this.get("id");
      this.set("_view_shops", String.join(",", S._for(ShopService.getByUser(user_id)).map(s -> s.get("name"))));
    }
    return this;
  }

  public List<Shop> view_shops() {
    List<Shop> _view_shops;

    if ((_view_shops = this.get("_view_shops")) == null) {
      refineViewShops();
    }

    return this.get("_view_shops");
  }


  public User refineRoles() {
    if (this.get("_roles") == null) {
      this.set("_roles", App.rbac.forUser(this.id()));
    }
    return this;
  }

  public RBAC.Roles roles() {
    if (this.get("_roles") == null) {
      refineRoles();
    }
    return this.get("_roles");
  }

  public User shop(Shop shop) {
    this.set("shop_id", shop.id());
    this.set("_shop", shop);
    return this;
  }

  public User resetShop() {
    this.set("shop_id", null);
    return this;
  }

  public static List<User> analyze(InputStream _is) {
    //从excel中获取excel数据
    try (final InputStream is = _is) {
      XLSTable table = new XLSTable(is);
      return table.parse(2, table.rows(), (row) -> {
        User user = new User();
        String rank = (String) row.get(0);
        String uid = (String) row.get(1);
        String password = (String) row.get(2);
        String name = (String) row.get(3);
        String phone = (String) row.get(4);
        String email = (String) row.get(5);
        String _sex = (String) row.get(6);
        String sex = null;
        if (STRING.isBlank(_sex) || _sex.equals("男")) sex = "0";
        else if (_sex.equals("女")) sex = "1";
        String shop = (String) row.get(7);
        user.set("uid", uid);
        user.set("password", password);
        user.set("name", name);
        user.set("phone", phone);
        user.set("email", email);
        user.set("sex", sex);
        user.set("shop", shop);
        user.set("rank", rank);
        return user;
      });
    } catch (IOException e) {
      throw new EndToEndException(400, "没有读取到XLS");
    }
  }

}
